package com.bdqn.zhao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bdqn.zhao.dao.IpFloorMapper;
import com.bdqn.zhao.pojo.IpFloor;
import com.bdqn.zhao.pojo.SysUser;
import com.bdqn.zhao.service.IpFloorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.sql.Date;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 * 楼层表 服务实现类
 * </p>
 *
 * @author zhangsan
 * @since 2023-04-24
 */
@Service
public class IpFloorServiceImpl extends ServiceImpl<IpFloorMapper, IpFloor> implements IpFloorService {

    @Autowired
    private IpFloorMapper ipFloorMapper;

    LocalDate date = LocalDate.now();
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    @Override
    public String selectAll(int buildId,int index,int count, HttpSession session) {
        QueryWrapper<IpFloor> ipParkQueryWrapper = new QueryWrapper<>();
        if(buildId!=0){
            ipParkQueryWrapper.eq("build_id", buildId);
        }

        Page<IpFloor> page = new Page<>(index, count);//设置分页参数
        IPage<IpFloor> ipFloorIPage = ipFloorMapper.selectPage(page, ipParkQueryWrapper);
        List<IpFloor> ipFloors = ipFloorIPage.getRecords();//查询到的分页数据
        session.setAttribute("ipFloorsList", ipFloors);
        session.setAttribute("ipFloorsCount", ipFloors.size());
        //当前页数
        session.setAttribute("current", page.getCurrent());
        session.setAttribute("pages", page.getPages());
        return "/ipFloor";
    }

    @Override
    public Boolean ipFloorsave(IpFloor ipFloor, HttpSession session) {
        ipFloor.setCreateBy("dong");
        ipFloor.setCreateTime(Date.valueOf(date.format(formatter)));
        return save(ipFloor);
    }

    @Override
    public Boolean updates(int floorId, IpFloor ipFloor, HttpSession session) {
        SysUser sysUser = (SysUser) session.getAttribute("User");
        ipFloor.setUpdateBy("dong");
        ipFloor.setUpdateTime(Date.valueOf(date.format(formatter)));
        UpdateWrapper<IpFloor> updateWrapper2 = new UpdateWrapper();
        updateWrapper2.eq("floor_id", floorId);
        return update(ipFloor,updateWrapper2);
    }

    @Override
    public Boolean deletefloor(int floorId) {
        return removeById(floorId);
    }

    @Override
    public Boolean deletefloors(int buildId) {
        return removeById(buildId);
    }


}
